package com.hmall.cart.listenel;

import com.hmall.cart.service.impl.CartServiceImpl;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

import java.util.Set;

@Configuration
@RequiredArgsConstructor
public class CartClearListenel {

    private final CartServiceImpl cartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue",durable = "true"),
            exchange = @Exchange(value = "cart.clear.direct",type = "direct"),
            key = "cart.clear"
    ))
    public void clearCart(Set<Long> itemIds, Message message) {
        System.out.println("cart监听到清理购物车的消息，现在开始清理购物车");
        Long userID = message.getMessageProperties().getHeader("user_info");
        UserContext.setUser(userID);
        cartService.removeByItemIds(itemIds);
    }
}
